<template>
	<view class="gift-package">
		<!-- 顶部标题 -->
		<view class="header" :style="{ background: t('color1') }">
			<text class="title">送礼清单</text>

			<view class="subtitle">支付后分享给好友收礼（2天内有效）</view>
		</view>

		<!-- 礼包类型选择 -->
		<view class="package-tabs">
			<view class="tab-item" v-for="(tab, index) in tabs" :key="index" @tap="switchTab(index)" :style="{
				color: currentTab === index ? t('color1') : ''
			}">
				<text class="tab-text">{{ tab.name }}</text>
			</view>

		</view>

		<!-- 礼包内容区 -->
		<view class="package-content">
			<!-- 单人礼包说明 -->
			<view class="package-desc" v-if="currentTab === 0">
				<view class="">
					<view class="desc-item">
						<image class="desc-icon" :src="static_url + '/static/img/ag_team.png'"></image>
					</view>
					<view class="desc-text">
						支持送给一个好友
					</view>
				</view>
				<view class="">
					<image class="desc-icon-right" src="../../static/img/arrowright.png"></image>
				</view>
				<view class="">
					<view class="desc-item">
						<image class="desc-icon" src="../../static/img/store.png"></image>
					</view>
					<view class="desc-text">
						礼包可包含多件商品
					</view>
				</view>
			</view>
			<!-- 多人礼包说明 -->
			<view class="package-desc" v-if="currentTab === 1">
				<view class="">
					<view class="desc-item">
						<image class="desc-icon" :src="static_url + '/static/img/ag_team.png'"></image>

					</view>
					<view class="desc-text">
						支持送给多个好友
					</view>
				</view>
				<view class="">
					<image class="desc-icon-right" src="../../static/img/arrowright.png"></image>
				</view>
				<view class="">
					<view class="desc-item">
						<image class="desc-icon" src="../../static/img/store.png"></image>
					</view>
					<view class="desc-text">
						每个好友领一个礼包
					</view>
				</view>
				<view class="">
					<image class="desc-icon-right" src="../../static/img/arrowright.png"></image>
				</view>
				<view class="">
					<view class="desc-item">
						<image class="desc-icon" src="../../static/img/store.png"></image>
					</view>
					<view class="desc-text">
						礼包可包含多件商品
					</view>
				</view>
			</view>
			<!-- 多人礼包说明 -->
			<view class="package-desc" v-if="currentTab === 2">
				<view class="">
					<view class="desc-item">
						<image class="desc-icon" :src="static_url + '/static/img/ag_team.png'"></image>

					</view>
					<view class="desc-text">
						支持同时送多个好友
					</view>
				</view>
				<view class="">
					<image class="desc-icon-right" src="../../static/img/arrowright.png"></image>
				</view>
				<view class="">
					<view class="desc-item">
						<image class="desc-icon" src="../../static/img/store.png"></image>
					</view>
					<view class="desc-text">
						每个好友领一个礼包
					</view>
				</view>
				<view class="">
					<image class="desc-icon-right" src="../../static/img/arrowright.png"></image>
				</view>
				<view class="">
					<view class="desc-item">
						<image class="desc-icon" src="../../static/img/store.png"></image>
					</view>
					<view class="desc-text">
						礼包奖品随机
					</view>
					<view class="desc-text-d">
						可为实物、无奖品
					</view>
				</view>
			</view>
			<!-- 隐私设置 -->
			<view class="privacy-setting" v-if="currentTab === 0 || currentTab === 1">
				<view class="setting-left">

					<text class="setting-text">是否要保持神秘感</text>
					<text class="setting-desc">收礼的隐藏商品信息</text>
				</view>
				<switch :checked="isPrivate" @change="onPrivacyChange" color="#4CD964" />
			</view>
			<!-- 人数设置 -->
			<view class="privacy-setting" v-if="currentTab === 1">
				<view class="setting-left">

					<text class="setting-text">收礼人数</text>

				</view>
				<view class="setting-right">
					<view class="gift-count">
						<text class="minus" @tap="updateNumCount(-1)">-</text>
						<text class="count">{{ NumCount }}</text>
						<text class="plus" @tap="updateNumCount(1)">+</text>
					</view>
				</view>
			</view>
			<!-- 抽奖设置-->
			<view class="privacy-setting" v-if="currentTab === 2">
				<view class="setting-left">

					<text class="setting-text">抽奖人数</text>
					<text class="setting-poper">抽奖人数等于奖品数</text>
				</view>
				<view class="setting-right">
					<view class="gift-count">
						<text class="gift-count-num">{{ totalCount }}</text>位好友
					</view>
				</view>
			</view>
			<!-- 礼品列表 -->
			<view class="gift-list">
				<view class="list-header">

					<text class="list-title">礼包商品</text>
					<text class="list-desc">可选择多件商品</text>
				</view>

				<!-- 已选商品 -->
				<view class="gift-item" v-for="(item, index) in selectedGifts" :key="index">
					<image class="gift-img" :src="item.pic" mode="aspectFill"></image>
					<view class="gift-info">
						<view class="gift-title">{{ item.name }}</view>

						<view class="gift-desc"> {{ item.nowguige.name }} </view>
						<view class="gift-price" :style="{ color: t('color2') }">
							¥{{ item.sell_price }} 
						<text style="margin-left: 10rpx;">×  {{item.num}}</text>
						</view>
					</view>
					<view class="gift-r">
						<view class="del-box" @click="delBtn(index)">
							<image class="del-img" src="/static/img/del.png" mode=""></image>
						</view>
						<!-- <view class="gift-count">
							<text class="minus" @tap="updateCount(index, -1)">-</text>
							<text class="count">{{ item.num }}</text>
							<text class="plus" @tap="updateCount(index, 1)">+</text>
						</view> -->
					</view>
				</view>

				<!-- 添加商品按钮 -->
				<view class="add-gift" :style="{ color: t('color1') }" @tap="addGift">
					<text class="add-icon">+</text>
					<text>添加商品</text>
				</view>
			</view>
			<view class="gift-card">
				<view class="coupon-box">
					<text class="f1">优惠券</text>
					<view class="conpou-right" @tap="openPopup">
						<view class="f2" :class="{ 'f2-col': couponBoxData.price }">
							{{ couponBoxData.price ? `-￥${couponBoxData.price}` : couponDefaultText }}
						</view>
						<text class="iconfont iconjiantou" style="color:#999;font-weight:normal"></text>
					</view>
				</view>
				<view v-for="(buydata, index) in allbuydata" :key="index" class="buydata">
					<view class="price-yf">
						<text class="f1">{{ buydata.freightList[buydata.freightkey].freight_price_txt || '运费'
											}}<text v-if="buydata.freightList[buydata.freightkey].pstype != 1 && buydata.freightList[buydata.freightkey].freeset == 1"
								style="color:#aaa;font-size:24rpx;">（满{{
														buydata.freightList[buydata.freightkey].free_price }}元包邮）</text></text>
						<text class="f2">+¥{{ buydata.freightList[buydata.freightkey].freight_price }}</text>
					</view>
				</view>
				<view class="gift-card-flex">
					<view class="">
						礼品份数
					</view>
					<view class="" v-if="currentTab == 0">
						1
					</view>
					<view class="" v-if="currentTab == 1">
						{{ NumCount }}
					</view>
					<view class="" v-if="currentTab == 2">
						{{ totalCount }}
					</view>
					<!-- <view class="">
						{{ totalCount }}
					</view> -->
				</view>
				<view class="gift-card-flex">
					<view class="">
						实付金额
					</view>
					<view class="" v-if="currentTab == 0">
						¥{{ totalPrice }} 
					</view>
					<view class="" v-if="currentTab == 1">
						¥{{ totalPrice * NumCount }} 
					</view>
					<view class="" v-if="currentTab == 2">
						¥{{ totalPrice * NumCount }} 
					</view>
				</view>
			</view>
			
			<!-- 底部结算栏 -->
			<view class="bottom-bar">
				<view class="">
					<view class="price-info">

						<text>实付金额</text>
						<text class="price" :style="{ color: t('color2') }" v-if="currentTab == 0">¥{{ totalPrice }}</text>
						<text class="price" :style="{ color: t('color2') }" v-if="currentTab == 1">¥{{ totalPrice * NumCount}}</text>
						<text class="price" :style="{ color: t('color2') }" v-if="currentTab == 2">¥{{ totalPrice }}</text>
					</view>
					<!-- <view class="tips-text" v-if="currentTab===0">
						送给
						<text class="tips-num">1</text>
						位好友，共领取<text class="tips-num">{{totalCount}}</text>件商品
					</view>
					<view class="tips-text" v-if="currentTab===1">
						送给<text class="tips-num">{{ NumCount }}</text>位好友，每人领取<text class="tips-num">{{totalCount}}</text>件商品
					</view>
					<view class="tips-text" v-if="currentTab===2">
						<text class="tips-num">{{NumCount}}</text>位好友抽奖，共抽取<text class="tips-num">{{totalCount}}</text>件商品
					</view> -->
				</view>
				<view class="pay-btn" :style="{ background: t('color1') }" @tap="onPay">立即付款</view>
			</view>
		</view>
		<view v-if="couponvisible" class="popup__container">
			<view class="popup__overlay" @tap.stop="handleClickMask"></view>
			<view class="popup__modal">
				<view class="popup__title">
					<text class="popup__title-text">请选择{{ t('优惠券') }}</text>
					<image src="/static/img/close.png" class="popup__close" style="width:36rpx;height:36rpx"
						@tap.stop="handleClickMask" />
				</view>
				<view class="popup__content card-content">
					<!-- <couponlist :couponlist="allbuydata[bid].couponList" :choosecoupon="true"
						:selectedrids="allbuydata[bid].couponrids" :bid="bid" @chooseCoupon="chooseCoupon">
					</couponlist> -->
					<scroll-view class="popup-lq" scroll-y="true">
						<block v-if="coupon_list.length > 0">
							<view class="card" :style="{ background: t('color2') }" v-for="(item, index) in coupon_list"
								:key="index">
								<view class="card-left">
									<view class="card-left-top" v-if="item.type == 41"
										:style="{ background: `linear-gradient(to right, ${t('color2')}, ${t('color1')})` }">
										新人券
									</view>
									<text class="card-left-money">￥</text>
									<text>{{ item.money }}</text>
								</view>
								<view class="card-item" @tap="selectCoupon(index)">
									<view class="card-right">
										<view class="card-time">
											{{ item.couponname }}
										</view>
										<view class="card-right-text">
											<text class="product-title"
												v-if="item.producttitle != null && item.producttitle !== undefined">
												{{ item.producttitle }}
											</text>
										</view>
										<view class="card-time">
											{{ calculateRemainingTime(item.starttime, item.endtime) }}
										</view>
									</view>
									<view class="card-btn-box">
										<view class="radiobox" @tap.stop="changeradio" :data-index="index">
											<view class="radio"
												:style="item.checked ? 'background:' + t('color1') + ';border:0' : ''">
												<image class="radio-img" src="/static/img/checkd.png" />
											</view>
										</view>
									</view>
								</view>
							</view>
						</block>
						<block v-else>
							<view class="no-coupon">
								<text>暂无可用优惠券</text>
							</view>
						</block>
					</scroll-view>
					<button v-if="coupon_list.length > 0" class="pop-btn" :style="{ background: t('color1') }"
						@tap.stop="confirmBtn">确定</button>

				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var app = getApp();
	export default {
		data() {
			return {
				pre_url: app.globalData.pre_url,
				static_url: app.globalData.static_url,
				currentTab: 0,
				NumCount: 1,
				tabs: [{
						name: '单人礼包',

					},
					{
						name: '多人礼包',

					},
					{
						name: '抽奖礼包',

					}
				],
				isPrivate: false, // 默认是公开礼物
				gift_is_secret: 2, // 默认值为 2 (公开礼物)
				selectedGifts: [],
				opt: {},
				pro_ids: [],
				couponBoxData: { // 传递给 coupon-box 的数据
					price: null, // 默认价格
					record_id: null, // 默认 record_id
				},
				couponDefaultText: '请选择优惠券',
				couponvisible: false,
				coupon_list: [],
				allbuydata: [],
				buydata: {
					freightList: [], // 初始化运费列表
					freightkey: 0 // 默认选择的运费索引
				},
				prodata:''
			}
		},
		onLoad(opt) {
			this.pro_ids = Array.isArray(app.globalData.pro_ids) ? app.globalData.pro_ids : [];
			this.selectedGifts = app.globalData.giftdata;
			this.currentTab = app.globalData.gift_currentTab;
			const prodata = this.selectedGifts
			  .map(item => `${item.proid},${item.ggid},${item.num}`)
			  .join('-');
			this.prodata = prodata;
			if ( opt.gift_is_secret && opt.gift_num) {
				
				this.isPrivate = app.globalData.gift_is_secret
				this.NumCount = app.globalData.gift_num
			}
			this.opt = opt;
			if (app.globalData.pro_ids.length > 0) {
				this.getproscoupon()
			}
			this.getdata()
		},

		computed: {
		    totalCount() {
		        return parseInt(this.selectedGifts.reduce((sum, item) => sum + item.num, 0), 10)
		    },
		    totalPrice() {
		        // 计算商品总价
		        const goodsTotal = this.selectedGifts.reduce((sum, item) => sum + item.sell_price * item.num, 0);
		        
		        // 计算总运费
		        const freightTotal = this.allbuydata.reduce((sum, buydata) => {
		            return sum + (buydata.freightList[buydata.freightkey].freight_price || 0);
		        }, 0);
		        
		        // 计算总金额（商品总价 + 运费）
		        const totalWithFreight = goodsTotal + freightTotal;
		        
		        // 如果有优惠券,减去优惠券金额
		        const couponDiscount = this.couponBoxData.price || 0;
		        
		        // 确保最终价格不小于0
		        const finalPrice = Math.max(0, totalWithFreight - couponDiscount);
		        
		        return finalPrice.toFixed(2);
		    }
		},

		methods: {
			getproscoupon() {
				var that = this;
				const pro_ids = app.globalData.giftdata.map(item => item.proid).join(',');
				app.get('coupon/getproscoupon', {
					pro_ids: pro_ids,
				}, function(res) {
					if (res.code == 200) {
						// 处理优惠券选中状态
						that.coupon_list = res.record_list.map(item => {
							// 判断当前优惠券是否应该被选中
							const isChecked = that.pro_ids.includes(item.couponid.toString());
							return {
								...item,
								checked: isChecked
							};
						});

						// 计算选中的优惠券总金额
						that.calculateTotalDiscount();

					} else {
						app.error(res.msg);
					}
				});
			},
			calculateTotalDiscount() {
				// 获取选中的优惠券
				const selectedCoupon = this.coupon_list.find(item => item.checked);

				if (selectedCoupon) {
					// 更新 couponBoxData
					this.couponBoxData = {
						price: selectedCoupon.money,
						record_id: selectedCoupon.record_id
					};
				} else {
					// 如果没有选中优惠券，清空数据
					this.couponBoxData = {
						price: 0,
						record_id: null
					};
				}
			},
			selectCoupon(index) {
				// 先将所有优惠券设置为未选中
				this.coupon_list.forEach(item => {
					item.checked = false;
				});

				// 设置当前选中的优惠券
				this.coupon_list[index].checked = true;

				// 更新 pro_ids,只保存当前选中的优惠券ID
				const couponId = this.coupon_list[index].couponid.toString();
				this.pro_ids = [couponId];

				// 更新 couponBoxData
				this.couponBoxData = {
					price: this.coupon_list[index].money,
					record_id: this.coupon_list[index].record_id
				};
			},
			confirmBtn() {
				// 获取选中的优惠券
				const selectedCoupon = this.coupon_list.find(item => item.checked);

				if (selectedCoupon) {
					// 判断是否满足最低购买金额条件
					if (this.totalPrice < selectedCoupon.minprice) {
						app.error(`订单金额需大于${selectedCoupon.minprice}才能使用此优惠券`);
						return;
					}

					// 判断是否满足最低购买数量条件
					if (this.totalCount < selectedCoupon.minnum) {
						app.error(`购买数量需大于${selectedCoupon.minnum}才能使用此优惠券`);
						return;
					}

					// 更新 pro_ids
					this.pro_ids = [selectedCoupon.couponid.toString()];

					// 更新 couponBoxData
					this.couponBoxData = {
						price: selectedCoupon.money,
						record_id: selectedCoupon.record_id
					};

					// 关闭弹窗
					this.couponvisible = false;
				} else {
					// 如果没有选中优惠券，清空数据
					this.pro_ids = [];
					this.couponBoxData = {
						price: 0,
						record_id: null
					};
					this.couponvisible = false;
				}
			},
			changeradio(event) {
				const index = event.currentTarget.dataset.index;
				this.selectCoupon(index);
			},
			openPopup() {
				var that = this;
				var pro_id = that.opt.prodata.split(',')[0];
				app.get('coupon/getprocoupon', {
					pro_id: pro_id,
				}, function(res) {
					if (res.code == 200) {
						// 保持之前选中的优惠券状态
						that.coupon_list = res.record_list.map(item => {
							// 检查当前优惠券是否在之前选中的列表中
							const isChecked = that.pro_ids.includes(item.couponid.toString());
							return {
								...item,
								checked: isChecked
							};
						});

						// 重新计算总优惠金额
						that.calculateTotalDiscount();

						that.couponvisible = true;
					} else {
						app.error(res.msg)
					}
				});
			},
			getPorcoupon() {
				var that = this;
				var pro_id = that.opt.prodata.split(',')[0];
				app.get('coupon/getprocoupon', {
					pro_id: pro_id,
				}, function(res) {
					if (res.code == 200) {
						that.coupon_list = res.record_list.map(item => {
							//默认选中使用优惠券
							if (that.opt.youhui_coupon_id == item.couponid) {
								item.checked = true;
								that.selectedCoupon = item;
								that.confirmBtn();
							}
							return {
								...item,
								checked: that.selectedCoupon && item.record_id === that.selectedCoupon
									.record_id,
							};
						});
					} else {
						app.error(res.msg)
					}
				});
			},
			calculateRemainingTime(starttime, endtime) {
				// 转换日期格式为 iOS 支持的格式
				const formatDateForIOS = (dateString) => {
					return dateString.replace(/-/g, '/');
				};

				const now = new Date();
				const startTime = new Date(formatDateForIOS(starttime)); // 转换 starttime
				const endTime = new Date(formatDateForIOS(endtime)); // 转换 endtime

				console.log('时间:', now, startTime, endTime);

				if (now >= startTime && now <= endTime) {
					const timeDiff = endTime - now;
					const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
					const hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
					const minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
					return `仅剩${days}天${hours}小时${minutes}分钟`;
				} else if (now < startTime) {
					return "优惠券尚未生效";
				} else {
					return "优惠券已过期";
				}
			},
			handleClickMask() {
				this.couponvisible = false;
				// 保持已选中的优惠券状态，不清空
			},
			getdata: function() {
				var that = this;
				that.loading = true;
				app.get('Shop/buy', {
					prodata: that.prodata,
					mendian_id: that.mendian_id
				}, function(res) {
					that.loading = false;
					if (res.status == 0) {
						if (res.msg) {
							app.alert(res.msg, function() {
								if (res.url) {
									app.goto(res.url);
								} else {
									app.goback();
								}
							});
						} else if (res.url) {
							app.goto(res.url);
						} else {
							app.alert('您没有权限购买该商品');
						}
						return;
					}

					that.havetongcheng = res.havetongcheng;
					that.address = res.address;
					if (that.address && that.address.latitude) {
						that.latitude = that.address.latitude;
						that.longitude = that.address.longitude;
					}
					that.linkman = res.linkman;
					that.tel = res.tel;
					that.userinfo = res.userinfo;
					that.buy_selectmember = res.buy_selectmember;
					that.order_change_price = res.order_change_price;
					that.pstype3needAddress = res.pstype3needAddress;
					if (that.buy_selectmember) {
						uni.request({
							url: app.globalData.static_url + '/static/area2.json',
							data: {},
							method: 'GET',
							header: {
								'content-type': 'application/json'
							},
							success: function(res2) {
								that.items = res2.data
							}
						})
					}
					that.allbuydata = res.allbuydata;
					that.bid = res.bid;
					that.business_payconfirm = res.business_payconfirm || false;
					that.allbuydatawww = JSON.parse(JSON.stringify(res.allbuydata));
					that.needLocation = res.needLocation;
					that.scorebdkyf = res.scorebdkyf;
					that.multi_promotion = res.multi_promotion;
					that.showprice_dollar = res.price_dollar
					that.usdrate = res.usdrate
					that.hasglassproduct = res.hasglassproduct
					that.business_selfscore = res.business_selfscore || 0;
					that.scoredkdataArr = res.scoredkdataArr || {};
					if (res.moneydec) {
						that.moneydec = res.moneydec;
						var shop_num = 0;
						var shop_bid = 0;
						//获取对象数量
						for (var key in res.allbuydata) {
							console.log(key);
							shop_num++
							shop_bid = key;
						}
						that.shop_num = shop_num;
						that.shop_bid = shop_bid;

					}

					that.loaded();


					//自动使用余额抵扣

					// var allbuydata = that.allbuydata;
					// for (var i in allbuydata) {
					// 	allbuydata[i].tempInvoice = uni.getStorageSync('temp_invoice_' + allbuydata[i].bid);
					// }
					// that.allbuydata = allbuydata;


					if (res.needLocation == 1) {
						app.getLocation(function(res) {
							var latitude = res.latitude;
							var longitude = res.longitude;
							that.latitude = latitude;
							that.longitude = longitude;
							var allbuydata = that.allbuydata;
							for (var i in allbuydata) {
								var freightList = allbuydata[i].freightList;
								for (var j in freightList) {
									if (freightList[j].pstype == 1 || freightList[j].pstype == 5) {
										var storedata = freightList[j].storedata;
										if (storedata) {
											for (var x in storedata) {
												if (latitude && longitude && storedata[x].latitude &&
													storedata[x].longitude) {
													var juli = that.getDistance(latitude, longitude,
														storedata[x].latitude, storedata[x]
														.longitude);
													storedata[x].juli = juli;
												}
											}
											storedata.sort(function(a, b) {
												return a["juli"] - b["juli"];
											});
											for (var x in storedata) {
												if (storedata[x].juli) {
													storedata[x].juli = storedata[x].juli + '千米';
												}
											}
											allbuydata[i].freightList[j].storedata = storedata;
										}
									}
								}
							}
							that.allbuydata = allbuydata;
						});
					}
				});
			},
			switchTab(index) {
				this.currentTab = index;
				app.globalData.gift_currentTab = index;
			},
			delBtn(index) {
			    if (index !== -1) {
			        app.confirm('确定移除当前商品吗?', () => {
			            // 1. 获取要删除的商品
			            const deletedItem = this.selectedGifts[index];
			            
			            // 2. 从 selectedGifts 中删除
			            this.selectedGifts.splice(index, 1);
			            
			            // 3. 更新 app.globalData.giftdata（移除对应的商品）
			            const giftIndex = app.globalData.giftdata.findIndex(
			                item => item.proid === deletedItem.proid && item.ggid === deletedItem.ggid
			            );
			            if (giftIndex !== -1) {
			                app.globalData.giftdata.splice(giftIndex, 1);
			            }
			            
			            // 4. 重新计算 prodata（基于最新的 selectedGifts）
			            const prodata = this.selectedGifts
			                .map(item => `${item.proid},${item.ggid},${item.num}`)
			                .join('-');
			            
			            // 5. 更新 this.prodata 和 app.globalData.prodata
			            this.prodata = prodata;
			            app.globalData.prodata = prodata;
			            
			            // 6. 如果需要，重新获取数据（例如刷新 UI）
			            if (this.getdata) {
			                this.getdata();
			            }
			        });
			    }
			},
			onPrivacyChange(e) {
				this.isPrivate = e.detail.value;
				if (this.isPrivate) {
					// 如果是神秘礼物 (true)，设置 gift_is_secret 为 1
					this.gift_is_secret = 1;

				} else {
					// 如果是公开礼物 (false)，设置 gift_is_secret 为 2
					this.gift_is_secret = 2;

				}
				console.log(this.gift_is_secret);
			},

			updateCount(index, delta) {
				console.log(index, delta);
				const newCount = Number(this.selectedGifts[index].num) + delta

				if (newCount >= 1) {
					this.selectedGifts[index].num = newCount
				}
			},
			updateNumCount(index) {
				const newCount = this.NumCount + index;
				// 确保人数不小于 0
				if (newCount > 0) {
					this.NumCount = newCount;
				}
			},
			addGift() {
				let currentTab = this.currentTab
				app.globalData.gift_is_secret = this.isPrivate
				app.globalData.gift_num = this.NumCount
				uni.reLaunch({
					url: './gift_zone?currentTab=' + currentTab + '&type=2' 
				})
			},



			//创建订单
			onPay() {
				let that = this
				let order_type = 2
				let gift_is_secret = that.gift_is_secret
				let gift_type = that.currentTab
				let originalData = that.selectedGifts
				let gift_total_num = that.NumCount
				const prodata = originalData.map(item => {

					return `${item.proid},${item.nowguige.id},${item.num}`;
				}).join('-');
				var couponrid = that.couponBoxData.record_id;
				const buydata = [{
					"bid": 0,
					"prodata": prodata,
					"couponrid": couponrid,
					"freight_id": 12,
					"freight_time": "",
					"storeid": 0,
					"formdata": {
						"form0": ""
					},
					"type11key": 0,
					"moneydec_rate": 0
				}];

				

				//单人
				if (that.currentTab == 0) {
					app.post('Shop/createOrder', {
						frompage: "",
						buydata: buydata,
						addressid: '',
						linkman: '',
						tel: '',
						usescore: '',
						latitude: '',
						longitude: '',
						order_type: 2,
						gift_total_num: 1,
						gift_type: 1,
						gift_is_secret: gift_is_secret,
					}, function(res) {
						app.showLoading(false);
						if (res.status == 0) {
							//that.showsuccess(res.data.msg);
							app.error(res.msg);
							return;
						}
						if (res.status == 10) {
							uni.showModal({
								title: '提示',
								content: res.msg,
								success: function(ress) {
									if (ress.confirm) {
										// 执行确认后的操作
										app.globalData.giftdata = []
										app.globalData.gift_is_secret = false
										app.globalData.gift_num = 1
										app.goto('/pages/pay/pay?id=' + res.payorderid +
											'&order_type=' + order_type);
									} else {
										// 执行取消后的操作functionlares.data.payorderid
									}
								}
							})
							return;
						}
						app.error('订单编号：' + res.payorderid);
						if (res.payorderid)
							app.globalData.giftdata = []
						app.globalData.gift_is_secret = false
						app.globalData.gift_num = 1
						app.goto('/pages/pay/pay?id=' + res.payorderid + '&order_type=' + order_type);
					});

				}
				//多人
				if (that.currentTab == 1) {
					app.post('Shop/createOrder', {
						frompage: "",
						buydata: buydata,
						addressid: '',
						linkman: '',
						tel: '',
						usescore: '',
						latitude: '',
						longitude: '',
						order_type: 2,
						gift_total_num: gift_total_num,
						gift_type: 2,
						gift_is_secret: gift_is_secret,
					}, function(res) {
						app.showLoading(false);
						if (res.status == 0) {
							//that.showsuccess(res.data.msg);
							app.error(res.msg);
							return;
						}
						if (res.status == 10) {
							uni.showModal({
								title: '提示',
								content: res.msg,
								success: function(ress) {
									if (ress.confirm) {
										// 执行确认后的操作
										app.globalData.giftdata = []
										app.globalData.gift_is_secret = false
										app.globalData.gift_num = 1
										app.goto('/pages/pay/pay?id=' + res.payorderid +
											'&order_type=' + order_type);
									} else {
										// 执行取消后的操作
									}
								}
							})
							return;
						}
						app.error('订单编号：' + res.payorderid);
						if (res.payorderid)
							app.globalData.giftdata = []
						app.globalData.gift_is_secret = false
						app.globalData.gift_num = 1
						app.goto('/pages/pay/pay?id=' + res.payorderid + '&order_type=' + order_type);
					});
				}
				//抽奖
				if (that.currentTab == 2) {
					app.post('Shop/createOrder', {
						frompage: "",
						buydata: buydata,
						addressid: '',
						linkman: '',
						tel: '',
						usescore: '',
						latitude: '',
						longitude: '',
						order_type: 2,
						gift_total_num: that.totalCount,
						gift_type: 3,
						gift_is_secret: gift_is_secret,
					}, function(res) {
						app.showLoading(false);
						if (res.status == 0) {
							//that.showsuccess(res.data.msg);
							app.error(res.msg);
							return;
						}
						if (res.status == 10) {
							uni.showModal({
								title: '提示',
								content: res.msg,
								success: function(ress) {
									if (ress.confirm) {
										// 执行确认后的操作
										app.globalData.giftdata = []
										app.globalData.gift_is_secret = false
										app.globalData.gift_num = 1
										app.goto('/pages/pay/pay?id=' + res.payorderid +
											'&order_type=' + order_type);
									} else {
										// 执行取消后的操作
									}
								}
							})
							return;
						}
						app.error('订单编号：' + res.payorderid);
						if (res.payorderid)
							app.globalData.giftdata = []
						app.globalData.gift_is_secret = false
						app.globalData.gift_num = 1
						app.goto('/pages/pay/pay?id=' + res.payorderid + '&order_type=' + order_type);
					});
				}

			}
		}
	}
</script>

<style>
	.gift-package {
		min-height: 100vh;
		background: #f8f8f8;
		padding-bottom: 120rpx;
	}

	.header {
		background: rgb(186, 31, 37);
		color: #fff;
		padding: 30rpx 20rpx;
		position: relative;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
	}

	.subtitle {
		font-size: 24rpx;
		margin-top: 10rpx;
		opacity: 0.8;
	}

	.guide-btn {
		position: absolute;
		right: 20rpx;
		top: 30rpx;
		background: rgba(255, 255, 255, 0.2);
		padding: 8rpx 20rpx;
		border-radius: 30rpx;
		font-size: 24rpx;
	}

	.package-tabs {
		display: flex;
		background: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}

	.tab-item {
		flex: 1;
		text-align: center;
		padding: 20rpx 0;
		border-right: 1rpx solid #eee;
	}

	.tab-item:last-child {
		border-right: none;
	}

	.tab-item.active {
		color: #4CD964;
	}

	.tab-text {
		font-size: 28rpx;
		font-weight: bold;
		display: block;
	}

	.tab-desc {
		font-size: 22rpx;
		color: #999;
		margin-top: 10rpx;
		display: block;
	}

	.package-desc {
		display: flex;
		background: #fff;
		padding: 30rpx 20rpx;
		margin-bottom: 20rpx;
		align-items: center;
		justify-content: center;
	}


	.desc-icon {
		width: 80rpx;
		height: 80rpx;

	}

	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		text-align: center;
	}

	.desc-item {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-right: 10px;
	}

	.desc-text {
		color: #000000;
		font-size: 24rpx;
		text-align: center;
	}

	.desc-text-d {
		color: #999;
		font-size: 24rpx;
		text-align: center;
	}

	.privacy-setting {
		background: #fff;
		padding: 30rpx 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.setting-left {
		display: flex;
		align-items: center;
	}

	.setting-num {
		width: 40rpx;
		height: 40rpx;
		line-height: 40rpx;
		text-align: center;
		background: #4CD964;
		color: #fff;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.setting-desc {
		font-size: 24rpx;
		color: #999;
		margin-left: 20rpx;
	}

	.setting-poper {
		font-size: 24rpx;
		color: #999;
		margin-left: 20rpx;
	}

	.gift-list {
		background: #fff;
		padding: 20rpx;
	}

	.list-header {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.list-num {
		width: 40rpx;
		height: 40rpx;
		line-height: 40rpx;
		text-align: center;
		background: #4CD964;
		color: #fff;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.list-desc {
		font-size: 24rpx;
		color: #999;
		margin-left: 20rpx;
	}

	.gift-item {
		display: flex;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.gift-img {
		width: 160rpx;
		height: 160rpx;
		border-radius: 8rpx;
		margin-right: 20rpx;
	}

	.gift-info {
		flex: 1;
	}

	.desc-icon-right {
		width: 40rpx;
		height: 40rpx;
	}

	.gift-title {
		font-size: 28rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
		display: -webkit-box;

		-webkit-line-clamp: 2;

		-webkit-box-orient: vertical;

		overflow: hidden;

		text-overflow: ellipsis;

	}

	.gift-desc {
		font-size: 24rpx;
		color: #999;
		margin-bottom: 10rpx;
		display: -webkit-box;

		-webkit-line-clamp: 2;

		-webkit-box-orient: vertical;

		overflow: hidden;

		text-overflow: ellipsis;
	}

	.gift-price {
		color: #ff6b81;
		font-size: 32rpx;
		font-weight: bold;
	}

	.gift-count {
		display: flex;
		align-items: center;
	}

	.gift-count-num {
		color: #4CD964;
	}

	.minus,
	.plus {
		width: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		background: #f5f5f5;
		border-radius: 50%;
	}

	.count {
		margin: 0 20rpx;
	}

	.add-gift {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 30rpx 0;
		color: #4CD964;
	}

	.add-icon {
		font-size: 32rpx;
		margin-right: 10rpx;
	}

	.gift-card {
		margin-top: 20rpx;
		padding: 20rpx;
		background-color: #fff;
	}

	.gift-card-flex {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.bottom-bar {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		background: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.price-info {
		font-size: 26rpx;
		color: #666;
	}

	.price {
		color: #ff6b81;
		font-size: 32rpx;
		font-weight: bold;
		margin-left: 10rpx;
	}

	.pay-btn {
		width: 200rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: #4CD964;
		color: #fff;
		border-radius: 40rpx;
		font-size: 28rpx;
	}

	.tips-text {
		color: #bfbfbf;
	}

	.tips-num {
		color: #FF6B81;
	}

	.del-img {
		width: 100%;
		height: 100%;
	}

	.gift-r {
		position: relative;
		display: flex;
		justify-content: center;
		align-items: center;

	}

	.del-box {
		width: 24rpx;
		height: 24rpx;
		position: absolute;
		top: 0;
		right: 0;
	}

	.gift-count {
		display: flex;
		justify-content: center;
		align-items: center;
		gap: 10px;
	}

	.coupon-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.conpou-right {
		display: flex;
		align-items: center;

	}

	.conpou-right .f2 {
		color: #999;
	}

	.right-icon {
		width: 40rpx;
		height: 40rpx;
	}

	.card-content {
		padding: 0 20rpx;
	}

	.card {
		display: flex;
		align-items: center;
		width: 100%;

		background: #fff;
		border-radius: 8px;
		-webkit-mask-image: radial-gradient(circle at 88px 4px, transparent 4px, red 4.5px), radial-gradient(closest-side circle at 50%, red 99%, transparent 100%);

		-webkit-mask-repeat: repeat, repeat-y;
		-webkit-mask-position: 0 -8rpx, 170rpx;

		position: relative;
		margin-bottom: 20rpx;
	}

	.card-left {
		width: 88px;
		text-align: center;
		font-size: 18px;
		color: #fff;
		flex-shrink: 0;
	}

	.card-left-top {
		position: absolute;
		top: 0;
		left: 0;
		font-size: 24rpx;
		padding: 4rpx 12rpx;
		border-bottom-right-radius: 30rpx;
	}

	.card-right {
		padding: 16px 12px;
		display: flex;
		// flex: 1;
		flex-direction: column;
	}

	.card-info {
		margin: 0;
		font-size: 14px;
		line-height: 20px;
		color: #fff;
	}

	.card-time {
		font-size: 12px;
		line-height: 16px;
		font-weight: normal;
		color: #fff;
		margin-top: 4px;
	}

	.card-right-text {
		display: flex;
		align-items: center;
		color: #fff;
	}

	.card-right-img {
		width: 30rpx;
		height: 30rpx;
	}

	.card-left-money {
		font-size: 24rpx
	}

	.card-btn-item {
		width: 180rpx;
	}

	.card-btn {
		color: #fff;
		border-radius: 30rpx;
		padding: 10rpx 30rpx;
		font-size: 24rpx;
		text-align: center;

	}

	.product-title {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		max-width: 240rpx;
	}

	.card-btn-text {
		font-size: 24rpx;
		color: #fff;
		text-align: center;
	}

	.card-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex: 1;
		padding: 0 20rpx 0 0;
	}

	.card-item::before {
		content: '';
		position: absolute;

		top: 0;
		bottom: 0;
		width: 2px;
		border-left: 1px dashed #fff;

	}

	.radiobox {
		position: absolute;
		right: 0;
		top: 40rpx;
		padding: 20rpx
	}

	.radio {
		flex-shrink: 0;
		width: 32rpx;
		height: 32rpx;
		background: #FFFFFF;
		border: 2rpx solid #BFBFBF;
		border-radius: 50%;
	}

	.radio-img {
		width: 100%;
		height: 100%
	}

	.pop-btn {
		border-radius: 36rpx;
		color: #fff;
	}

	.f2-col {
		color: #ff4c4c;
	}

	.no-coupon {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 200rpx;
		font-size: 28rpx;
		color: #999;
	}

	.popup__content {
		position: relative;
		padding-bottom: 120rpx;
		/* 为确定按钮留出空间 */
		height: 70vh;
		/* 弹框内容区域高度 */
	}

	.popup-lq {
		height: 100%;
		/* 滚动区域高度 */
		overflow-y: auto;
		-webkit-overflow-scrolling: touch;
	}

	.pop-btn {
		position: absolute;
		bottom: 20rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 90%;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		color: #fff;
		font-size: 32rpx;
		border-radius: 40rpx;
		z-index: 1000;
	}

	.price-yf {
		width: 100%;
		padding: 20rpx 0;
		background: #fff;
		display: flex;
		align-items: center
	}

	.price-yf .f1 {
		color: #333
	}

	.price-yf .f2 {
		color: #111;
		font-weight: bold;
		text-align: right;
		flex: 1
	}

	.price-yf .f3 {
		width: 24rpx;
		height: 24rpx;
	}

	.price-yf .couponname {
		color: #fff;
		padding: 4rpx 16rpx;
		font-weight: normal;
		border-radius: 8rpx;
		font-size: 24rpx;
		display: inline-block;
		margin: 2rpx 0 2rpx 10rpx
	}
</style>